package com.kifas.service.dataaccess;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.SupplierEntity;

@Repository
public class SupplierDao {

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(SupplierDao.class);

	private static String ALL_SUPPLIER = "from SupplierEntity sup ";
	private static String SUPPLIER_BY_ID = ALL_SUPPLIER
			+ "where sup.idSupplier = :idSupplier ";

	@Autowired
	private SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public List<SupplierEntity> getSuppliers() {
		return sessionFactory.getCurrentSession().createQuery(ALL_SUPPLIER)
				.list();
	}

	public SupplierEntity getSupplier(Long idSupplier) {
		return (SupplierEntity) sessionFactory.getCurrentSession()
				.createQuery(SUPPLIER_BY_ID).setLong("idSupplier", idSupplier)
				.uniqueResult();
	}
	
	public void insert(SupplierEntity supplier) throws Exception {
		sessionFactory.getCurrentSession().save(supplier);
	}
	
	public void update(SupplierEntity supplier) throws Exception {
		sessionFactory.getCurrentSession().update(supplier);
	}
}
